package com.insightg3.android;

import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Typeface;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class SearchResultAdapter extends ArrayAdapter {
    private ArrayList items; 
    private LayoutInflater inflater; 
// private static int i = 0;

    public SearchResultAdapter(Context context, int textViewResourceId, 
    ArrayList items) { 

	    super(context, textViewResourceId, items); 
	
	    this.items = items; 
	
	    this.inflater = (LayoutInflater) context 
	              .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
    } 

    

	@Override 
	public View getView(int position, View convertView, ViewGroup parent) { 
	
		// ビューを受け取る 
		View view = convertView; 
		
		if (view == null) { 
		
			// 受け取ったビューがnullなら新しくビューを生戄1�7 
			view = inflater.inflate(R.layout.main_result_row, null); 
			
			// 背景画像をセットする 
			view.setBackgroundResource(R.drawable.back); 
		} 
		
		view.setLongClickable(true);
			
		// 表示すべきデータの取径1�7 
		SearchResultStatus item = (SearchResultStatus)items.get(position);
		
		//.d("No", String.valueOf(i++));
		if (item != null) { 
			
			TextView screenName = (TextView)view.findViewById(R.id.toptext); 
			screenName.setTypeface(Typeface.DEFAULT_BOLD); 
			
			// スクリーンネームをビューにセット 
			TextView text = (TextView)view.findViewById(R.id.bottomtext); 
			
			if (screenName != null) { 
				screenName.setText(item.getTitle()); 
			} 
			

			// テキストをビューにセット 
			if (text != null) {  
				text.setText(item.getSummary()); 
			} 
			
			//price setting
			TextView txtprice = (TextView)view.findViewById(R.id.price);
			if (txtprice != null) {
				   txtprice.setText(item.getPrice()); 
			}

			//image setting
			ImageView imageView = (ImageView)view.findViewById(R.id.icon);
			try {
				imageView.setImageBitmap(AndroidUtils.returnBitMap(item.getImage(), 32, 32));
			} catch(NullPointerException e) {
				Log.d("Exception", String.valueOf(e));
			}
			  
		} 
		return view; 
	} 
/*
    @Override
    public View getView(int position, View convertView, ViewGroup parent) { 
    	View view = convertView;
        if (view == null) {
           	  view = inflater.inflate(R.layout.taobao_row, null);
           	 // view.setBackgroundResource(R.drawable.back);
        } 
        
        SearchResultStatus item = (SearchResultStatus)items.get(position);
        if (item != null) {
        	  
           	  TextView screenName = (TextView)view.findViewById(R.id.toptext); 
           	  screenName.setTypeface(Typeface.DEFAULT_BOLD);
           	  TextView text = (TextView)view.findViewById(R.id.bottomtext); 
           	  if (screenName != null) {
           		  screenName.setText(item.getTitle()); 
           	  } 
           	  if (text != null) {  
           		  text.setText(item.getSummary()); 
           	  } 
           	  
        	
        	if(item != null) {
    			ImageView image = (ImageView)view.findViewById(R.id.icon);			
    			TextView title = (TextView)view.findViewById(R.id.toptext);
    			TextView price = (TextView)view.findViewById(R.id.price);

    			if (image != null) {
    				image.setImageBitmap(returnBitMap(item.getImage()));
    			} else {
    				item.setTitle("image");
    			}
    			image.setImageBitmap(returnBitMap(item.getImage()));
    			if (title != null) {
    				title.setText(item.getTitle());
    			}
    			if (price != null) {
    				price.setText(item.getPrice() + "元");
    			}

    		}
        } 
        return view; 
    } 
    public Bitmap returnBitMap(String url) {   
		URL myFileUrl = null;   
		Bitmap bitmap = null;   
		try {   
			myFileUrl = new URL(url);   
		} catch (MalformedURLException e) {   
			e.printStackTrace();   
		}   
		try {   
			HttpURLConnection conn = (HttpURLConnection) 
			myFileUrl.openConnection();   
			conn.setDoInput(true);   
			conn.connect();   
			InputStream is = conn.getInputStream();   
			bitmap = BitmapFactory.decodeStream(is);   
			is.close();   
		} catch (IOException e) {   
			e.printStackTrace();   
		}   
		return bitmap;   
	}*/
}
